home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / apps / 521 / midipl02 / midiplay.c < prev    next >
C/C++ Source or Header  |  1991-04-17  |  3KB  |  121 lines

  1. /*
  2.  * File: midiplay.c
  3.  * SGoldthorpe     9-Apr-91
  4.  */
  5.  
  6. /*
  7.  * midiplay - play standard midi files (sometimes)
  8.  * FREEWARE (C) 1991 Stephen Goldthorpe - Take all you want!  But please it be
  9.  * known if you've made mods.  I don't want a lot of complaints about code I've
  10.  * never even seen before (life's hard enough without all of that)!
  11.  *                        SGoldthorpe.wgc-e@rx.xerox.com
  12.  *                             goldthor@arisia.xerox.com
  13.  */
  14.  
  15. /*
  16.  * REVISION LOG
  17.  * ============
  18.  * 0.1    SGoldthorpe    20-Mar-91    Created for Atari ST / Sozobon C.  It's
  19.  *                    a bit atari specific in places buy i've
  20.  *                    tried to make it UNIX(tm) looking for
  21.  *                    easier porting (if anyone feels brave
  22.  *                    enough to try.
  23.  * 0.2    SGoldthorpe     7-Apr-91    Messed up the code in mf_intp to
  24.  *                    allow type 1 midi files.  Timing is
  25.  *                    still a bit hairy but it plays 80%
  26.  *                    of the files I have OK.
  27.  */
  28.  
  29. #include    <stdio.h>
  30. #include    <types.h>
  31. #include    <stat.h>
  32. #include    <fcntl.h>
  33. #include    <malloc.h>
  34. #include    <errno.h>
  35.  
  36. /* MACRO DEFS */
  37. #define        RELEASE    0
  38. #define        VERSION    2
  39. #define        DATE    " 9-Apr-91"
  40.  
  41. /* EXTERNAL DECLS */
  42. extern int    mf_intp();
  43.  
  44. /* GLOBAL CONSTANTS */
  45. char    app_name[] =    "midiplay";
  46.  
  47. /* FUNCTION DECLS */
  48. static void    play();
  49.  
  50. /* MAIN CODE - SHORT & SWEET */
  51. int        main(argc,argv)
  52. int        argc;
  53. char        *argv[];
  54. {    int    file_n;
  55.  
  56.     /* title (so you and I know what's what) */
  57.     (void)printf("%s v %d.%d (%s)\n",app_name,RELEASE,VERSION,DATE);
  58.  
  59.     /* check args */
  60.     if(argc<2)
  61.     {    (void)fprintf(stderr,"%s: <file1> (...<fileN>) \n",app_name);
  62.         exit();
  63.     };
  64.  
  65.     /* play all files in arg list */
  66.     for(file_n=1;file_n<argc;file_n++)
  67.         play(argv[file_n]);
  68. };
  69.  
  70. /* FUNCTION DEFS */
  71. static void    play(file)
  72. char    *file;
  73. {    struct stat    stat_buff;
  74.     BYTE        *file_buff;
  75.     int        fd;
  76.     unsigned int    len;
  77. #ifdef    DEBUG
  78.     (void)fprintf(stderr,"%s: play %s\n",app_name,file);
  79. #endif
  80.  
  81.     /* open the file (is possible)  and get info */
  82.     if((fd=open(file,O_RDONLY))<0)
  83.     {    (void)fprintf(stderr,"%s: can't open %s\n",app_name,file);
  84.         return(-1);
  85.     };
  86.     if(stat(file,&stat_buff)<0)
  87.     {    perror(app_name);
  88.         return(-1);
  89.     };
  90.     len=(unsigned int)stat_buff.st_size;
  91.  
  92.     /* get some workspace... */
  93. #ifdef    DEBUG
  94.     (void)fprintf(stderr,"%s: malloc (%d)\n",app_name,len);
  95. #endif
  96.     if((file_buff=(BYTE*)malloc(len))==NULL)
  97.     {    (void)fprintf(stderr,
  98.             "%s: couldn't get enough memory for %s (%d bytes)\n",
  99.             app_name,file,len);
  100.         (void)close(fd);
  101.         return(-1);
  102.     };
  103.  
  104.     /* ...and fill it */
  105.     if(read(fd,(BYTE*)file_buff,len)!=len)
  106.     {    (void)fprintf(stderr,"%s: problem reading %s\n",app_name,file);
  107.         (void)close(fd);
  108.         free((char*)file_buff);
  109.         return(-1);
  110.     };
  111.     (void)close(fd);
  112.  
  113.     /* send it to mf_intp for the hard work */
  114.     (void)mf_intp(file_buff,file,len);
  115.  
  116.     /* and tidy up */
  117.     free((char*)file_buff);
  118. };
  119.  
  120. /* THAT'S ALL FOLKS! */
  121.